<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Create Login-Fieldset and store in QuickSkin
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
if (isset($_POST['func']))
{
	if ($validate->cleanslash($_POST['func']) === 'login')
	{
		// clear user input
		$inputUsername = $validate->cleanslash($_POST['username']);
		$inputPassword = $validate->cleanslash($_POST['password']);
		// Login user
		$login = loginUser($inputUsername, $inputPassword, $cart, $validate);

		// Sign in was correct
		if ($login)
		{
			// User identifiers store in DB
			$update = updateUser($inputUsername);
			header('location: index.php');
			exit;
		}
		else
		{
			// TODO Error-Page for Login-Errors
			$fehlermeldung = '<h3>Die Anmeldung war fehlerhaft!</h3>';
		}
	}
}

if (!checkUser())
{
	$page->assign('loginfield',
		'<form action="index.php" method="post" class="loginfield">
    <fieldset>
    <label for="username">' . _('Username') . ': </label><br>
    <input type="text" name="username" id="username" size="20" /><br>
    <label for="password">' . _('Password') . ': </label><br>
    <input type="password" name="password" id="password" size="20" autocomplete="off"/>
    <br><br>
    <input type="submit" name="submit" id="submit" value="' . _('Login') . '" />
	 <input type="hidden" name="func" value="login">
    </fieldset>
    </form>');

	if (USER_LOGIN)
	{
		$page->assign('registerLink',
			'<a title="' . _('New Register') .
			'"	href="/index.php?page=register&name=register' .
			'">' . _('New Register') . '</a>');
	}
	else
	{
		$page->assign('registerLink', '');
	}
	$page->assign('logintext', '');
	$page->assign('loginheader', _('Login / Register'));
}
else
{
	$page->assign('loginfield',
		'<form action="index.php" method="post" class="logoutfield">
    <fieldset>' .
		_('Hello') . ' ' . mysql_real_escape_string($_SESSION['customernamex']) .
		'<br>
    <input type="submit" name="submit" id="submit" value="' . _('Logout') . '" />
	 <input type="hidden" name="logout" value="logout">
    </fieldset>
    </form>');

	$page->assign('registerLink', '');
	$page->assign('logintext', '');
	$page->assign('loginheader', _('Logout'));
}
?>